/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import healthcare.coursework.model.Patient;
import healthcare.coursework.service.PatientService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericAutowireComposer;
import org.zkoss.zul.Button;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Div;
import org.zkoss.zul.Groupbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Panel;
import org.zkoss.zul.Window;

/**
 *
 * @author Administrator
 */
public class PatientManagement extends GenericAutowireComposer {

    //Componennt
    private Combobox trackCodeSearch;
    private Button patientQuery;
    private Label fullName;
    private Label birthdate;
    private Label address;
    private Label phone;
    private Label job;
    private Label trackcode;
    private Panel viewPatientForm;
    private Groupbox patientFunction;
     
    //Service
    private PatientService patientService = util.Utility.getUtility().getPatientService();
    private List<Patient> patients;
    private Patient patient;

    public void onLoadForm(Event e) {
       
        loadTrackingCode();
        setInfo();

    }

    /**
     * get patient id from url 
     */
    private void initPatientInfo(){
         Long patientId = Long.valueOf(execution.getParameter("patientId"));
         patient=patientService.findPatient(patientId);
    }

    private void setInfo() {
        setInformation();
    }

    public void onPatientQuery(Event event) {
        try {
            String trackCode = trackCodeSearch.getText();
            if (trackCode.trim().isEmpty()) {
                Messagebox.show("Track code cannot be empty", "Warning", Messagebox.OK, Messagebox.EXCLAMATION);
                return;
            }
            patient=patientService.findPatientByTrackingCode(trackCode);
            System.out.println("Load patient: " + patient.getTrackCode());
            setInformation();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    private void loadTrackingCode() {
        patients = patientService.findPatients();
        trackCodeSearch.setModel(new ListModelList(patients, true));
    }

    public void setInformation() {
        try {

            System.out.println("Enter Info: " + patient.getPatientId());
            trackcode.setValue(patient.getTrackCode());
            fullName.setValue(patient.getFullName());
            birthdate.setValue(patient.getDateOfBirth().toString());
            job.setValue(patient.getJob());
            address.setValue(patient.getAddress());
            phone.setValue(patient.getPhone());
            viewPatientForm.setVisible(true);
            patientFunction.setVisible(true);
        } catch (Exception e) {
            viewPatientForm.setVisible(false);
            patientFunction.setVisible(false);
        }
    }

    public void onViewMedicalHistory(Event event) {
        if (patient != null) {
            execution.sendRedirect("ViewMedicalHistories.do?patientId=" + patient.getPatientId());
        } else {
            System.out.println("Patient is null value. Cannot redirect page");
        }
    }

    public void onAddMedicalHistory(Event e) {
        try {
            Map params = new HashMap();
            params.put("patient", patient);
            Window win = (Window) execution.createComponents(
                    "/WEB-INF/zul/pages/doctor/AddMedicalHistory.zul", null, params);
            win.setMaximizable(true);
            win.setClosable(true);
            win.doModal();

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    public void onArrangeLabTesting(Event e) {
        try {
            Map params = new HashMap();
            params.put("patient", patient);
            Window win = (Window) execution.createComponents(
                    "/WEB-INF/zul/pages/doctor/ArrangeLabTesting.zul", null, params);
            win.setMaximizable(true);
            win.setClosable(true);
            win.doModal();

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}
